﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FCNS.Controls
{
     /// <summary>
    /// FCNS.Controls.ListView 的标题
    /// </summary>
    internal partial class Title : System.Windows.Forms.Control
    {
		Bitmap _offBmp = null;
		Pen pen1;
		SolidBrush sb1;
		string title = "";
        /// <summary>
        /// 实例化 ListView 的新实例
        /// </summary>
		internal Title()
		{
			pen1 = new Pen(Color.Empty);
			sb1 = new SolidBrush(Color.Empty);

			this.SetStyle(ControlStyles.DoubleBuffer, true);
			this.SetStyle(ControlStyles.UserPaint, true);
			this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			this.SetStyle(ControlStyles.UserMouse, true);
			this.SetStyle(ControlStyles.ResizeRedraw, false);
			this.SetStyle(ControlStyles.Opaque, true);
         
            InitializeComponent();
        }

        private void Title_Resize(object sender, EventArgs e)
        {
            Repaint2();
        }

        private void Title_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(_offBmp, 0, 0);
        }

        void Repaint2()
        {
            if ((this.ClientRectangle.Width < 1) || (this.ClientRectangle.Height < 1))
            {
                return;
            }
            if ((_offBmp == null) || (_offBmp.Height != this.ClientRectangle.Height) || (_offBmp.Width != this.ClientRectangle.Width))
            {
                if (_offBmp != null)
                {
                    _offBmp.Dispose();
                }
                _offBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            }
            Graphics _g = Graphics.FromImage(_offBmp);

            _g.Clear(Color.White);
            PaintWidget(_g);
            _g.Dispose();
            Paint2();
        }

        void Paint2()
        {
            if ((_offBmp == null) || (_offBmp.Height != this.ClientRectangle.Height) || (_offBmp.Width != this.ClientRectangle.Width))
            {
                Repaint2();
                return;
            }
            if ((this.ClientRectangle.Width < 1) || (this.ClientRectangle.Height < 1))
            {
                return;
            }
            IntPtr hwnd = new IntPtr();
            hwnd = this.Handle;
            Graphics drawingSurface = Graphics.FromHwnd(hwnd);
            drawingSurface.DrawImage(_offBmp, 0, 0);
            drawingSurface.Dispose();
        }

        void DrawFillVGradient(Graphics g_, Rectangle rect1, Color color1, Color color2)
        {
            System.Int32 r, g, b;
            for (int i = 0; i < rect1.Height; i++)
            {
                r = color2.R + ((((rect1.Height - 1) - i) * (color1.R - color2.R)) / (rect1.Height - 1));
                g = color2.G + ((((rect1.Height - 1) - i) * (color1.G - color2.G)) / (rect1.Height - 1));
                b = color2.B + ((((rect1.Height - 1) - i) * (color1.B - color2.B)) / (rect1.Height - 1));
                pen1.Color = Color.FromArgb(255, r, g, b);
                g_.DrawLine(pen1, rect1.Left, rect1.Top + i, rect1.Right - 1, rect1.Top + i);
            }
        }

        void PaintWidget(Graphics g)
        {
            Rectangle rect1 = new Rectangle(0, 0, this.ClientRectangle.Width, 15);
            DrawFillVGradient(g, rect1, Color.FromArgb(255, 233, 233, 233), Color.FromArgb(255, 139, 139, 139));

            pen1.Color = Color.FromArgb(255, 102, 102, 102);
            g.DrawLine(pen1, 0, 15, this.ClientRectangle.Width, 15);

            Font font1 = new Font("Tahoma", 8, FontStyle.Bold);
            StringFormat align1 = new StringFormat();
            align1.Alignment = StringAlignment.Center;
            sb1.Color = Color.FromArgb(255, 255, 255, 255);
            g.DrawString(title, font1, sb1, (this.ClientRectangle.Width / 2) - 1, ((this.ClientRectangle.Height - font1.Height) / 2) + 1, align1);
            sb1.Color = Color.FromArgb(255, 0, 0, 0);
            g.DrawString(title, font1, sb1, this.ClientRectangle.Width / 2, (this.ClientRectangle.Height - font1.Height) / 2, align1);
            font1.Dispose();
            align1.Dispose();
        }
        /// <summary>
        /// 获取或设置标题文本
        /// </summary>
        public string NameTitle
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
                Repaint2();
            }
        }
    }
}
